package com.aiit.service.impl;

import com.aiit.config.ShiroEncryption;
import com.aiit.dao.UserDao;
import com.aiit.pojo.Patient;
import com.aiit.pojo.User;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:UserService
 * Package:com.aiit.service
 * Description:
 *
 * @Date:2020/4/12 16:16
 * @Author:xjs
 */
@Service
public class UserService {
    @Resource
    private UserDao userDao;

    public List<User> findUserContent(){
        return userDao.findUserContent();
    }
    public List<User> findById(int userid){
        return userDao.findById(userid);
    }
    public int addTotal(){
        return userDao.addTotal();
    }
    public List<Patient> doctorFindPatients(){
        return userDao.doctorFindPatients();
    }
    public User selectUserName(String username){
        return userDao.selectName(username);
    }
    public int userRoleId(int userid){
        return userDao.userRoleId(userid);
    }
    public int userRegist(User user){
        // shiro 自带的工具类生成salt
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();

        String encodedPassword = ShiroEncryption.shiroEncryption(user.getPassword(),salt);//加盐的密码
        user.setSalt(salt);
        user.setPassword(encodedPassword);
        userDao.userRegist(user);
        //int userid = user.getUserid();//获取返回自增的id
        int result = userDao.addUserRole(user.getUserid(),user.getRole().getRoleid());
        return result;
    }
    public List<User> search(String action,String keyword){
        return userDao.search(action, keyword);
    }

    public int findNameByid(String username){
        return userDao.findNameByid(username);
    }

    public User getUserByEmail(User user) {
        return userDao.getUserByEmail(user);
    }
    public int forgetPwd(User user){
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        String encodedPassword = ShiroEncryption.shiroEncryption(user.getPassword(),salt);//加盐的密码
        user.setSalt(salt);
        user.setPassword(encodedPassword);
        return userDao.forgetPwd(user);
    }
}
